﻿/*
 * Copyright (c) Seagull Consulting, Inc. All rights reserved. See License.txt in the project root for license information.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using SeagullConsulting.MvcAjaxControls.Data.Models;

namespace SeagullConsulting.MvcAjaxControls.Data.Repository
{
    public class PersonRepository
    {
        IList<Person> people;

        public PersonRepository()
        {
            people = new List<Person>() {
                new Person() { Id = 1, FirstName = "Warren", LastName = "Wiltshire", Address = "4201 Bayshore Blvd, #803", CityId = 1,
                    City = new City { Name = "Tampa"},
                    Birthday = Convert.ToDateTime("1962/04/27 04:00:00Z"),
                    Income = 150000f,
                    Phones = new List<Phone>() { new Phone() { Id = 1, Number = "(813) 258-9423", PersonId = 1 },
                                                new Phone() { Id = 2, Number = "(813) 317-8254", PersonId = 1 }}},
                new Person() { Id = 2, FirstName = "Albert", LastName = "Adams", Address = "1313 Mockingbird Lane", CityId = 2,
                    City = new City { Name = "Brandon" },
                    Birthday = Convert.ToDateTime("06/24/2012 09:00:00Z"),
                    Income = 1f,
                    Phones = new List<Phone>() { new Phone() { Id = 3, Number = "(813) 555-1234", PersonId = 2 },
                                                new Phone() { Id = 4, Number = "(813) 555-1212", PersonId = 2 }}},
                new Person() { Id = 3, FirstName = "Test", LastName = "User1", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 4, FirstName = "Test", LastName = "User2", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 5, FirstName = "Test", LastName = "User3", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 6, FirstName = "Test", LastName = "User4", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 7, FirstName = "Test", LastName = "User5", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 8, FirstName = "Test", LastName = "User6", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 9, FirstName = "Test", LastName = "User7", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 10, FirstName = "Test", LastName = "User8", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 11, FirstName = "Test", LastName = "User9", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 12, FirstName = "Test", LastName = "User10", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 13, FirstName = "Test", LastName = "User11", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 14, FirstName = "Test", LastName = "User12", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 15, FirstName = "Test", LastName = "User13", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 16, FirstName = "Test", LastName = "User14", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 17, FirstName = "Test", LastName = "User15", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 18, FirstName = "Test", LastName = "User16", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 19, FirstName = "Test", LastName = "User17", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 20, FirstName = "Test", LastName = "User18", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 21, FirstName = "Test", LastName = "User19", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() },
                new Person() { Id = 22, FirstName = "Test", LastName = "User20", Birthday = DateTime.Now, Income = 1f, Address = String.Empty, CityId = 0, City = new City() }
            };
        }

        public IQueryable<Person> List()
        {
            //Thread.Sleep(2000);
            return people.OrderBy(c => c.LastName).ThenBy(c => c.FirstName).AsQueryable();
        }

        public IQueryable<Person> List(Func<Person, bool> expression)
        {
            Thread.Sleep(2000);
            return people.Where(expression).OrderBy(c => c.LastName).ThenBy(c => c.FirstName).AsQueryable();
        }

        public IQueryable<Person> List(PagerParms pagerParms)
        {

            Thread.Sleep(2000);
            return people.OrderBy(c => c.LastName)
                    .ThenBy(c => c.FirstName)
                    .Skip((pagerParms.PageNbr) * pagerParms.PageSize)
                    .Take(pagerParms.PageSize)
                    .AsQueryable();
        }

        public IQueryable<Person> List(PagerParms pagerParms, Func<Person, bool> expression)
        {

            Thread.Sleep(2000);
            return people.Where(expression)
                    .OrderBy(c => c.LastName)
                    .ThenBy(c => c.FirstName)
                    .Skip((pagerParms.PageNbr) * pagerParms.PageSize)
                    .Take(pagerParms.PageSize)
                    .AsQueryable();
        }

        public int ListCount()
        {
            return people.Count();
        }

        public int ListCount(Func<Person, bool> expression)
        {
            return people.Where(expression).Count();
        }

        public Person GetById(int id)
        {
            return people.FirstOrDefault(p => p.Id == id);
        }
    }
}